package com.modudf.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileUtils
{
  private static FileUtils instance = null;
  
  public static final synchronized FileUtils getInstance()
  {
    if (instance == null) {
      instance = new FileUtils();
    }
    return instance;
  }
  
  public String read(File file)
    throws IOException
  {
    return read(file, null);
  }
  
  public String read(File file, String charset)
    throws IOException
  {
    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = charset == null ? new InputStreamReader(fis) : new InputStreamReader(fis, charset);
    
    BufferedReader br = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    char[] buff = new char[2048];
    int len = 0;
    while ((len = br.read(buff)) != -1) {
      sb.append(buff, 0, len);
    }
    br.close();
    return sb.toString();
  }
}

